package com.zhang.sparksql_1

import org.apache.spark.SparkConf
import org.apache.spark.sql._

/**
 * @title:
 * @author: zhang
 * @date: 2021/12/10 19:48 
 */
object SparkSQl_HIVE_req {
  def main(args: Array[String]): Unit = {
    //  获取SparkSession连接对象
    val conf = new SparkConf().setMaster("local[*]").setAppName("spark-hive")
    val spark: SparkSession = SparkSession.builder().enableHiveSupport().config(conf).getOrCreate()
    //spark.sql("create database db_hive").show()
    spark.sql("use db_hive").show()
    spark.sql(
      """
        |CREATE TABLE `user_visit_action`(
        |  `date` string,
        |  `user_id` bigint,
        |  `session_id` string,
        |  `page_id` bigint,
        |  `action_time` string,
        |  `search_keyword` string,
        |  `click_category_id` bigint,
        |  `click_product_id` bigint,
        |  `order_category_ids` string,
        |  `order_product_ids` string,
        |  `pay_category_ids` string,
        |  `pay_product_ids` string,
        |  `city_id` bigint)
        |row format delimited fields terminated by '\t'
        |""".stripMargin)
    spark.sql(
      """
        |load data local inpath 'datas/user_visit_action.txt' into table user_visit_action
        |""".stripMargin)

    spark.sql(
      """
        | CREATE TABLE `product_info`(
        |      `product_id` bigint,
        |      `product_name` string,
        |      `extend_info` string)
        |    row format delimited fields terminated by '\t'
        |""".stripMargin)

    spark.sql(
      """
        |load data local inpath 'datas/product_info.txt' into table product_info
        |""".stripMargin)

    spark.sql(
      """
        |CREATE TABLE `city_info`(
        |      `city_id` bigint,
        |      `city_name` string,
        |      `area` string)
        |    row format delimited fields terminated by '\t'
        |""".stripMargin)
    spark.sql(
      """
        | load data local inpath 'datas/city_info.txt' into table city_info
        |""".stripMargin)

    spark.close()
  }
}
